/**
 * @Auther: ChenforCode
 * @Date: 2018/9/12 09:44
 * @Description:
 */
package cn.chenforcode.service;

import cn.chenforcode.dao.AdminBookRecordDAO;
import cn.chenforcode.dao.BookDAO;
import cn.chenforcode.entity.AdminBookRecord;
import cn.chenforcode.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookDAO bookDAO;

    @Autowired
    private AdminBookRecordDAO adminBookRecordDAO;

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成增加书籍的逻辑
     * @Date 9:55 2018/9/12
     * @Param [book, aname]
     **/
    public String addBook(Book book, String aname) {
        if (bookDAO.queryCanAdd(book.getBno()) == 1) {
            //增加书籍
            bookDAO.addBook(book);
            //增加记录
            AdminBookRecord adminBookRecord = new AdminBookRecord(aname, book.getBno(), new Timestamp(new Date().getTime()));
            adminBookRecordDAO.addAdminBookRecord(adminBookRecord);
            return "success";
        }
        return "failed";
    }

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成删除书籍的逻辑
     * @Date 9:59 2018/9/12
     * @Param [bno, aname]
     **/
    public String deleteBook(String bno, String aname) {
        if (bookDAO.queryCanDelete(bno) == 1) {
            //删除书籍
            bookDAO.deleteBookByBno(bno);
            //增加记录
            AdminBookRecord adminBookRecord = new AdminBookRecord(aname, bno, new Timestamp(new Date().getTime()));
            adminBookRecordDAO.addAdminBookRecord(adminBookRecord);
            return "success";
        }
        return "failed";
    }

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成更新书籍的相关逻辑
     * @Date 10:07 2018/9/12
     * @Param [book, aname]
     **/
    public String updateBook(Book book, String aname) {
        bookDAO.updateBook(book);
        AdminBookRecord adminBookRecord = new AdminBookRecord(aname, book.getBno(), new Timestamp(new Date().getTime()));
        adminBookRecordDAO.addAdminBookRecord(adminBookRecord);
        return "success";
    }

    /**
     * @return java.util.List<cn.chenforcode.entity.Book>
     * @Author ChenforCode
     * @Description //TODO 得到所有的书籍（管理员使用）
     * @Date 10:08 2018/9/12
     * @Param [aname]
     **/
    public List<Book> getAllBooks(String aname) {
        AdminBookRecord adminBookRecord = new AdminBookRecord(aname, "all", new Timestamp(new Date().getTime()));
        adminBookRecordDAO.addAdminBookRecord(adminBookRecord);
        return bookDAO.getAllBooks();
    }


    /**
     * @return java.util.List<cn.chenforcode.entity.Book>
     * @Author ChenforCode
     * @Description //TODO 得到所有的书籍（普通使用）
     * @Date 8:23 2018/9/14
     * @Param []
     **/
    public List<Book> getAllBooks() {
        return bookDAO.getAllBooks();
    }

    /**
     * @return cn.chenforcode.entity.Book
     * @Author ChenforCode
     * @Description //TODO 得到单个书籍（管理员使用）
     * @Date 10:09 2018/9/12
     * @Param [bno, aname]
     **/
    public Book getBook(String bno, String aname) {
        AdminBookRecord adminBookRecord = new AdminBookRecord(aname, bno, new Timestamp(new Date().getTime()));
        adminBookRecordDAO.addAdminBookRecord(adminBookRecord);
        return bookDAO.getBookByBno(bno);
    }

    /**
     * @return cn.chenforcode.entity.Book
     * @Author ChenforCode
     * @Description //TODO 得到单个书籍（普通使用）
     * @Date 8:24 2018/9/14
     * @Param [bno]
     **/
    public Book getBook(String bno) {
        return bookDAO.getBookByBno(bno);
    }
}
